<?php

/**
 * This is the model class for table "User".
 *
 * The followings are the available columns in table 'User':
 * @property string $id
 * @property string $name
 * @property string $password
 * @property string $salt
 * @property integer $fbuid
 * @property string $email
 * @property integer $admin
 * @property integer $status
 * @property string $created
 * @property string $updated
 * @property integer $deleted
 */
class User extends CActiveRecord
{
	const STATUS_UNACTIVATED = 0;
	const STATUS_ACTIVATED = 1;
	const STATUS_BANNED = 2;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className the class name
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'User';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		return array(
			array('name', 'required'),
			array('password', 'required', 'on'=>'insert'),
			array('admin, deleted', 'numerical', 'integerOnly'=>true),
			array('name, password', 'length', 'max'=>255),
			array('email', 'email'),
			array('salt, admin, status, created, updated', 'safe'),
			array('id, email, name, created, updated', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		return array(
			'entries'=>array(self::HAS_MANY, 'creatorId', 'Entry'),
		);
	}

	/**
	 * @return array attached behaviors.
	 */
	public function behaviors()
	{
		return array(
			'ext.auditbehavior.AuditBehavior',
			'ext.deletedbehavior.DeletedBehavior'
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => Yii::t('label','Id'),
			'email' => Yii::t('label','E-mail'),
			'name' => Yii::t('label','Name'),
			'password' => Yii::t('label','Password'),
			'fbuid' => Yii::t('label','Facebook User Id'),
			'status' => Yii::t('label','Status'),
			'created' => Yii::t('label','Created'),
			'updated' => Yii::t('label','Updated'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('t.id',$this->id,true);
		$criteria->compare('t.email',$this->email,true);
		$criteria->compare('t.name',$this->name,true);
		$criteria->compare('t.status',$this->status,true);
		$criteria->compare('t.created',$this->created,true);
		$criteria->compare('t.updated',$this->updated,true);
		$criteria->compare('t.deleted',$this->deleted);

		$criteria->addCondition('t.deleted=0');

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Actions to take before saving this record.
	 * @return boolean whether the record can be saved
	 */
	protected function beforeSave()
	{
		if (parent::beforeSave())
		{
			$this->salt = $this->generateSalt();
			$this->password = $this->hashPassword($this->password, $this->salt);
			return true;
		}
		else
			return false;
	}


	/**
	 * Checks if the given password is correct.
	 * @param string $password the password to be validated
	 * @return boolean whether the password is valid
	 */
	public function validatePassword($password)
	{
		return $this->hashPassword($password, $this->salt) === $this->password;
	}

	/**
	 * Generates the password hash.
	 * @param string $password the password
	 * @param string $salt the salt
	 * @return string the hashed password
	 */
	public function hashPassword($password,$salt)
	{
		return md5($salt.$password);
	}

	/**
	 * Generates a salt that can be used to generate a password hash.
	 * @return string the salt
	 */
	protected function generateSalt()
	{
		return uniqid('', true);
	}
}